package com.hooper.user.controller.api;

import com.hooper.hosp.model.user.Patient;
import com.hooper.hospital.common.result.Result;
import com.hooper.hospital.common.utils.AuthContextHolder;
import com.hooper.user.service.PatientService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/10/07/19:10
 */
@Api("patient api controller")
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/api/user/patient/auth")
public class PatientApiController {
    private final PatientService patientService;

    @GetMapping("findAll")
    public Result<List<Patient>> patientList(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.listByUserId(userId + "");
        return Result.ok(list);
    }

    @PostMapping("save")
    public Result save(@RequestBody Patient patient, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    @GetMapping("/get/{id}")
    public Result<Patient> getPatientById(@PathVariable String id) {
        return Result.ok(patientService.getById(id));
    }

    @PostMapping("/update")
    public Result updateById(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.ok();
    }

    @DeleteMapping("remove/{id}")
    public Result deleteById(@PathVariable String id) {
        patientService.removeById(id);
        return Result.ok();
    }
}
